﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// 玩家最终的结果
public enum PlayResult
{
	init,
	wait4input,
	fail,
	normal,
	good
}

public class LV1Rules : MonoBehaviour {
	public LV1CharaAction partner1;
	public LV1CharaAction partner2;
	public LV1Player player;
	public PressJudgment prsJdgm;
	protected PlayResult lastRst;
    public AudioSource bkgMusic;
    public AudioSource RUready;
	public Rythem rythem;
	public UILabel lTime;
	public UILabel lScore;
	public float sliderDeltaDis;
	public JagementTip tip;

	// Use this for initialization
	void Start () {
		if (null != player)
			//player.evtPlayerSing += new System.EventHandler<SingEventArg> (PlayerSing);

		ResetResult ();

		PlayerScore.Score = 0;

		prsJdgm.evtPrsJdg += new System.EventHandler<PrsJdgEventArgs> (OnJdgEvent);

		rythem.TimeStart ();
	}


	// Update is called once per frame
	void Update () {
		if(Input.GetKey(KeyCode.Escape)){
			Application.Quit();
		}

		if (rythem) {
			lTime.text = "time:" + (Time.time - rythem.Timeline).ToString();
		}

		lScore.text = "Score:" + PlayerScore.Score;
	}
    
	public void OnJdgEvent(object send, PrsJdgEventArgs args)
	{
		switch(args.jgRst)
		{
		case JageResult.Perfect:
			PlayGood();
			break;
		case JageResult.Good:
			PlayNormal();
			break;
		case JageResult.Fail:
			PlayFail();
			break;
		default:
			break;
		}
	}

    // 关卡结束后进入评分画面
    void End()
    {
        Application.LoadLevel("mrend");
    }

    // 播放背景音乐
	public void PlayBackgroundMusic()
    {
        if(null != bkgMusic)
            bkgMusic.Play();

		rythem.TimeStart ();
    }

	public void PlayRUready()
    {
        if (null != RUready)
            RUready.Play();
    }

	// AI开始唱歌
	public void Parter1Sing (int pitchlevel){
		partner1.Sing (pitchlevel);
	}

	public void Parter2Sing (int pitchlevel){
		partner2.Sing (pitchlevel);
	}

	public void Parter1Shout()
    {
        partner1.Shout();
    }

	public void Parter2Shout()
    {
        partner2.Shout();
    }

	// AI停止唱歌
	public void ParterShutup (int num)
	{
		if (0 == num % 2) {
			partner1.Shutup ();
		} else {
			partner2.Shutup ();
		}
	}

	// 最早开始判定玩家输入的时间点,也是一个环节的起始点，在这个函数调用前玩家唱歌都算错，该函数调用后才能开始唱歌
	/*public void Time2PlayerSing (SingAction type) {
		if (null != prsJdgm)
			prsJdgm.BeginAnalysis (type);
		SetResult (PlayResult.wait4input);
	}*/

	public void Time2PlayerSing (InputAction type) 
	{
		if (null != prsJdgm)
			prsJdgm.BeginAnalysis (type);
		SetResult (PlayResult.wait4input);
	}

	// 开启检测玩家输入的章节，设置人物唱歌的音调
	public void StartCheck()
	{
		ResetResult ();
	}

	public void SetPlayerPitch(int level)
	{
		player.pitchLevel = level;
	}

	// 一个环节结束，查看结果
	public void FinalCheck()
	{
		if (!prsJdgm.IsEmpty ()) {
			prsJdgm.RemoveAllJMonitor ();
			SetResult(PlayResult.fail);
		}

		if (PlayResult.fail == lastRst || PlayResult.wait4input == lastRst) {
			Despise();
		}
	} 

	void Despise()
	{
		partner1.Despise ();
		partner2.Despise ();
	}

	// 玩家唱歌
    /*void PlayerSing (object send, SingEventArg args){
		if (null != prsJdgm) {
			prsJdgm.GetResult (args.Singing);
		}
	}*/

	public void PlayGood()
	{
		Debug.Log("Play Good");

		SetResult (PlayResult.good);

		PlayerScore.Score += 3;

		tip.ShowUpResult (JageResult.Perfect);
	}

	public void PlayNormal()
	{
		Debug.Log("Play Normal");

		SetResult (PlayResult.normal);

		PlayerScore.Score += 1;

		tip.ShowUpResult (JageResult.Good);
	}

	public void PlayUnknown()
	{
		SetResult (PlayResult.fail);
		Despise ();
	}

	public void PlayFail()
	{
		SetResult (PlayResult.fail);
		Despise ();

		tip.ShowUpResult (JageResult.Fail);
	}

	void SetResult(PlayResult rst)
	{
		if (PlayResult.fail == lastRst) {
			return;
		}

		lastRst = rst;
	}

	void ResetResult()
	{
		lastRst = PlayResult.init;
	}
}

